package com.jixiang.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jixiang.dictionary.dao.DsLayoutImgDao;
import com.jixiang.model.common.dtos.ResponseResult;
import com.jixiang.model.dictionary.entity.DsLayoutImg;
import com.jixiang.dictionary.service.DsLayoutImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static com.jixiang.common.constants.RedisConstants.REDIS_PREX_DSLAYOUTIMG;

/**
 * (DsLayoutImg)表服务实现类
 *
 * @author makejava
 * @since 2023-10-17 00:19:49
 */
@Service("dsLayoutImgService")
public class DsLayoutImgServiceImpl extends ServiceImpl<DsLayoutImgDao, DsLayoutImg> implements DsLayoutImgService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public ResponseResult appGetLayoutCarouselList() {
        String jsonObj = stringRedisTemplate.opsForValue().get(REDIS_PREX_DSLAYOUTIMG);
        if(StrUtil.isNotBlank(jsonObj)){
            List<DsLayoutImg> list = JSONUtil.toList(jsonObj, DsLayoutImg.class);
            return ResponseResult.okResult(list);
        }
        List<DsLayoutImg> list = query().orderByAsc("priority").list();
        JSONArray objects = JSONUtil.parseArray(list);
        stringRedisTemplate.opsForValue().set(REDIS_PREX_DSLAYOUTIMG,objects.toString());
        return ResponseResult.okResult(list);
    }
}

